local duwu = fk.CreateSkill {

  name = "joy__duwu",

  tags = {  },

}



duwu:addEffect("active", {
  name = "joy__duwu",
  anim_type = "offensive",
  target_num = 1,
  can_use = function(self, player)
    return player:getMark("@@duwu-turn") == 0
  end,
  card_filter = Util.TrueFunc,
  target_filter = function(self, player, to_select, selected)
    if #selected == 0 and to_select ~= player.id then
      local target = Fk:currentRoom():getPlayerById(to_select)
      if target.hp == #selected_cards then
        if table.contains(selected_cards, player:getEquipment(Card.SubtypeWeapon)) then
          return player:distanceTo(target) == 1  --FIXME: some skills(eg.gongqi, meibu) add attackrange directly!
        else
          return player:inMyAttackRange(target)
        end
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:throwCard(effect.cards, duwu.name, player)
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = duwu.name,
    }
  end,
})

duwu:addEffect(fk.EnterDying, {
  name = "#joy__duwu_trigger",
  mute = true,
  events = {fk.EnterDying, fk.AfterDying},
  can_trigger = function(self, event, target, player, data)
    return data.damage and data.damage.skillName == "joy__duwu" and data.damage.from and data.damage.from == player and not player.dead
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    if event == fk.EnterDying then
      data.extra_data = data.extra_data or {}
      data.extra_data.duwu = player.id
    elseif data.extra_data.duwu == player.id and not target.dead then
      local room = player.room
      room:setPlayerMark(player, "@@duwu-turn", 1)
      if not player.dead then
        player:drawCards(1,"joy__duwu")
      end
    end
  end,
})
duwu:addEffect(fk.AfterDying, {
  name = "#joy__duwu_trigger",
  mute = true,
  events = {fk.EnterDying, fk.AfterDying},
  can_trigger = function(self, event, target, player, data)
    return data.damage and data.damage.skillName == "joy__duwu" and data.damage.from and data.damage.from == player and not player.dead
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    if event == fk.EnterDying then
      data.extra_data = data.extra_data or {}
      data.extra_data.duwu = player.id
    elseif data.extra_data.duwu == player.id and not target.dead then
      local room = player.room
      room:setPlayerMark(player, "@@duwu-turn", 1)
      if not player.dead then
        player:drawCards(1,"joy__duwu")
      end
    end
  end,
})

return duwu